package indonesian;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(9, 9, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<>(25000));
//    private static AtomicInteger integer = new AtomicInteger(1);

    private enum Column {
        EXAMPLE(0),
        UNKNOWNWORD_ALREADY_DOEN(1),
        UNKNOWNWORD_CORRECT_ALREADY_DOEN(2),
        ID(3),
        UNKNOWN_WORDS(4);
        int value;

        Column(int value) {
            this.value = value;
        }

        public int value() {
            return value;
        }
    }

    public  void main(String[] args) {
        String path = "F:\\IdeaProjects\\tagging3\\src\\test\\java\\indonesian\\印尼语待标注语料_04.xlsx";
        try (DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
             XSSFWorkbook workbook = new XSSFWorkbook(dataInputStream);
             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tagging2?serverTimezone=GMT%2B8", "root", "123456");) {
            Sheet sheetAt = workbook.getSheetAt(0);
            Iterator<Row> iterator = sheetAt.iterator();
            PreparedStatement statement1 = connection.prepareStatement(
                    "insert into tagging2.tagging_indonesian(id,example) values (?,?)");
            PreparedStatement statement2 = connection.prepareStatement(
                    "insert into tagging2.tagging_indonesian_unknown_word(tag_id,unknown_word)values (?,?)");
            String example = null;
            String unknownWords = null;
//            AtomicReference<AtomicReferenceArray<String>> unknownWordsAlreadyDone = new AtomicReference<>(new AtomicReferenceArray<>(new String[0]));
//            AtomicReference<AtomicReferenceArray<String>> unknownWordsAlreadyDoneCorrect = new AtomicReference<>(new AtomicReferenceArray<>(new String[0]));
            int id = 1;
            while (iterator.hasNext()) {
                Row row = iterator.next();
                try {
                    example = row.getCell(Main.Column.EXAMPLE.value).getStringCellValue();
                } catch (Exception e) {
//                    e.printStackTrace();
                    System.out.println("skip example->" + id);
                    continue;
                }
                try {
                    unknownWords = row.getCell(Main.Column.UNKNOWN_WORDS.value()).getStringCellValue();
                } catch (Exception e) {
                    System.out.println("skip unknownWords->" + id);
                    continue;
                }
                try {
                    statement1.setInt(1, id);
                    statement1.setString(2, example);
                    statement1.execute();
                    String[] unknownWordList = unknownWords.split(";");
                    for (String unknownWord : unknownWordList) {
                        statement2.setInt(1, id);
                        statement2.setString(2, unknownWord);
                        statement2.execute();
                    }
                    System.out.println(id++);
                } catch (Exception e) {
                    System.out.println("ERROR->" + example + "->" + id + e.getMessage());
                    continue;
                }
            }
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }
}
